package com.guigu.controller.design;


import com.guigu.pojo.design.DLevelFirst;
import com.guigu.pojo.design.DLevelSecond;
import com.guigu.pojo.design.DLevelThird;
import com.guigu.pojo.stock.SThirdProductArea;
import com.guigu.service.design.DLevelFirstService;
import com.guigu.service.design.DLevelSecondService;
import com.guigu.service.design.DLevelThirdService;
import com.guigu.service.stock.SThirdProductAreaService;
import com.guigu.utils.CodeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Mr.Zhang
 * @since 2024-12-17
 */
@Controller
@RequestMapping("/d-level-third")
public class DLevelThirdController {
    @Autowired
    private DLevelFirstService dLevelFirstService;

    @Autowired
    private DLevelSecondService dLevelSecondService;

    @Autowired
    private DLevelThirdService dLevelThirdService;

    @Autowired
    private SThirdProductAreaService sThirdProductAreaService;

    @GetMapping("/initialLevel")
    @ResponseBody
    public ArrayList<DLevelThird> initialLevel(HttpServletRequest req) {
        String secondId = StringUtils.isNotBlank(req.getParameter("secondId")) ? req.getParameter("secondId") : null;
        ArrayList<DLevelThird> dLevelThirds = dLevelThirdService.selectLevelThirdBySecondId(secondId);
        return dLevelThirds;
    }

    @RequestMapping("/initialLevelIII")
    @ResponseBody
    public ArrayList<DLevelThird> initialLevelIII(){
        ArrayList<DLevelThird> dLevelThirds = dLevelThirdService.selectLevelThird();
        System.out.println(dLevelThirds);
        return dLevelThirds;
    }

    @GetMapping("/initSeondSelect")
    @ResponseBody
    public ArrayList<DLevelSecond> initSelect(){
        ArrayList<DLevelSecond> dLevelSeconds = dLevelSecondService.selectLevelSecond();
        return dLevelSeconds;
    }

    @GetMapping("/initFirstSelect")
    @ResponseBody
    public ArrayList<DLevelFirst> initFirstSelect(){
        ArrayList<DLevelFirst> dLevelFirsts = dLevelFirstService.selectAllLevelFirst();
        return dLevelFirsts;
    }

    @PostMapping("/insertThird")
    @ResponseBody
    public Boolean insertThird(@RequestBody DLevelThird third){
        DLevelThird dLevelThird = dLevelThirdService.selectDLevelThirdOrderByDescOne();
        CodeUtils codeUtils = new CodeUtils();
        String newKindId = codeUtils.getNewKindId(dLevelThird.getThirdId());
        third.setThirdId(newKindId);
        Boolean b = dLevelThirdService.insertThird(third);

        SThirdProductArea sThirdProductArea = new SThirdProductArea();
        sThirdProductArea.setParentId(third.getParentId());
        sThirdProductArea.setParentId(third.getThirdId());
        sThirdProductArea.setThirdName(third.getThirdName());
        Boolean b2 = sThirdProductAreaService.insertSthirdProduct(sThirdProductArea);
        if(b && b2){
            return true;
        }
        return false;
    }
}

